package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.hp.hpl.jena.vocabulary.RDF;

import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Pattern;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.TriplePattern;

public class RelationRule extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));

			Relation rel = (Relation) request.getSession().getAttribute(
					"relation" + id);
			
			JSONArray elements = new JSONArray();

			Pattern pat = rel.getPattern();
			for (TriplePattern t : pat.getTriples()) {
				JSONObject jsob = new JSONObject();
				JSONArray variables = new JSONArray();
//				jsob.put("stext", t.getSubjectText());
//				jsob.put("ptext", t.getPropertyText());
//				jsob.put("otext", t.getObjectText());
//				System.out.println(t.getPropertyText()+"||||"+RDF.type.toString());
				if(t.getPropertyText().contentEquals(RDF.type.toString())){
					jsob.put("ptext", t.getObjectText());
//					JSONObject vitem = new JSONObject();
//					vitem.put("v", t.getSubjectText());
//					variables.add(vitem);
					variables.add(t.getSubjectText());
					jsob.put("vtext", variables);
				}else{
					jsob.put("ptext", t.getPropertyText());
					variables.add(t.getSubjectText());
					variables.add(t.getObjectText());
//					JSONObject vitems = new JSONObject();
//					vitems.put("v", t.getSubjectText());
//					variables.add(vitems);
//					JSONObject vitemo = new JSONObject();
//					vitemo.put("v", t.getObjectText());
//					variables.add(vitemo);
					jsob.put("vtext", variables);
				}
//				System.out.println("js:"+jsob.toJSONString());
				jsob.put("s", t.getSubject());
				jsob.put("p", t.getProperty());
				jsob.put("o", t.getObject());
				jsob.put("canDel",
						pat.canDel(t.getSubject(), t.getProperty(),
								t.getObject()));
				elements.add(jsob);
			}
//			String name = rel.getName();
			JSONArray hvariables = new JSONArray();
			for (String v : rel.getVariables()) {
				JSONObject jsob = new JSONObject();
				String role = rel.getVariableRole(v);
				if(role != null && role.trim().length()>0){
					jsob.put("v", v);
					jsob.put("show", 1);//display
				}else if(id==0){
					jsob.put("v", v);
					jsob.put("show", 1);//display
				}else{
					jsob.put("v", v);
					jsob.put("show", 0);//hide
				}
				hvariables.add(jsob);
			}
//			String hvariables = "";
//			for (String v : rel.getVariables()) {
//				JSONObject jsob = new JSONObject();
//				String role = rel.getVariableRole(v);
//				if(role != null && role.trim().length()>0){
////					head += role+",";
//					hvariables += v + ",";
//				}else if(id==0){
//					hvariables += v+",";
//				}
//			}
//			if(hvariables.contains(",")){
//				hvariables = hvariables.substring(0,hvariables.length()-1);
//			}
			JSONObject result = new JSONObject();
			result.put("head", hvariables);
			result.put("elements", elements);
			response.getWriter().print(result);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
